package com.kkb.utils.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author mj
 * @create 2021-03-23 14:42
 * @describe:
 */
public class DateUtils {

    public static String dateToStr(String rule, Date date){
        SimpleDateFormat format = new SimpleDateFormat(rule);
        return format.format(date);
    }

    public static String dateToStr( Date date){
        if (date==null){
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String dateToStrMiao( Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 获取昨天的日期
     * @return
     */
    public static String yesterday(){
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(calendar.getTime());
    }

    /**
     * 根据参数查询，距离现在几天了
     * @param param
     */
    public static int parseDateToNow(String param){
        //将字符串转为日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date= null;
        try {
            date = sdf.parse(param);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //将时间转为毫秒
        long s1=date.getTime();
        //得到当前的毫秒
        long s2=System.currentTimeMillis();

        long day=(s2-s1)/1000/60/60/24;
        return (int) day;
    }

    public static String parseDateHuanbi(String param){
        //将字符串转为日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date= null;
        try {
            date = sdf.parse(param);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //将时间转为毫秒
        long s1=date.getTime();
        //得到当前的毫秒
        long s2=System.currentTimeMillis();
        long day=(s2-s1)/1000/60/60/24;

        long huanbi = s1 - 1000*60*60*24*day;

        Date huanbiDate = new Date(huanbi);
        return sdf.format(huanbiDate);
    }
}
